import VueRouter from "vue-router";
import Main from '../views/Main'
import Home from '../views/Home'
import User from '../views/User'
import Mall from '../views/Mall'
import PageOne from '../views/PageOne'
import PageTwo from '../views/PageTwo'
import Login from '../views/Login'
import Cookie from "js-cookie";

const router = new VueRouter({
    routes:[
        {
            path:'/',
            component:Main,
            name:'Main',
            redirect:'/home',//重定向(意思是当路径为/时就让它重定向到home)
            children:[
                // {path:'user',name:'user',component:User},
                // {path:'home',name:'home',component:Home},
                // {path:'mall',name:'mall',component:Mall},
                // {path:'page1',name:'page1',component:PageOne},
                // {path:'page2',name:'page2',component:PageTwo},
            ]
        },
        {
            path:'/login',
            name:'login',
            component:Login
        }
    ]
})

//添加全局前置导航守卫
router.beforeEach((to,from,next)=>{
    // 判断token存不存在
    const token = Cookie.get('token')
    // 不存在则未登录，跳转到登录页
    if (!token && to.name != 'login') {
        next({name:'login'})
    }else if(token && to.name === 'login'){  //存在，说明登录了
        next({name:'home'})
    }else{
        next()
    }
})

export default router